//
// inOutMsgCenter.h
// ~~~~~~~~~~~~~~
//
// The center to process incoming request and output related replay.

#ifndef IN_OUT_MSG_CENTER_H
#define IN_OUT_MSG_CENTER_H

#include <string>
#include <iostream>

#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>

#include "message.h"

typedef boost::interprocess::message_queue * MsgQueuePtr;

class RecvdMsg
{
public:
    Message msg ;
    std::size_t rxSize ;
    unsigned int priority ;
} ;

/// Represents a single InOutMsgCenter from a client.
class InOutMsgCenter
            : public boost::enable_shared_from_this<InOutMsgCenter>,
            private boost::noncopyable
{
public:
    /// Construct a InOutMsgCenter with the name.
    InOutMsgCenter( const std::string & inMsgQName_, const std::string & outMsgQName_)
    	{
    	//Erase previous message queue
        boost::interprocess::message_queue::remove(outMsgQName_.c_str());

		inMsgQ = new boost::interprocess::message_queue(boost::interprocess::open_only, inMsgQName_.c_str()) ;
        outMsgQ = new boost::interprocess::message_queue(boost::interprocess::create_only, outMsgQName_.c_str(), maxMsgQueueCapacity, maxMsgSize) ;
    
        inMsgQName = inMsgQName_;
        outMsgQName = outMsgQName_;
    	}
	
	~InOutMsgCenter()
		{
		if (inMsgQ)
			{
			delete inMsgQ ;
			inMsgQ = NULL ;
			}
		if (outMsgQ)
			{
			delete outMsgQ ;
			outMsgQ = NULL ;
			}
		}

    void startRxMsg(RecvdMsg & inMsg) ;
    void sendReply(const Message & outReply) ;
    void sendReply(const void * buffer, std::size_t buffer_size, 
           unsigned int priority) ;

private:
    std::string inMsgQName ;
    MsgQueuePtr inMsgQ ;
    std::string outMsgQName ;
    MsgQueuePtr outMsgQ ;
       
    static const std::size_t maxMsgQueueCapacity = 100 ; //max message number
    static const std::size_t maxMsgSize = 512 ;
};



#endif // IN_OUT_MSG_CENTER_H
